 /**
 * 逻辑、功能相关描述:
 *
 * @author 王浩羽 编写日期：Dec 29, 2008
 */
package com.ce.pms.system.occupation.service.impl;

import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.impl.AbstractCustomerEventServiceImpl;
import com.ce.pms.system.occupation.dao.IContractorInfoConfigDao;
import com.ce.pms.system.occupation.model.ContractorInfoConfigDO;
import com.ce.pms.system.occupation.service.IContractorInfoConfigService;
import com.ce.pms.system.occupation.vo.ContractorInfoConfigVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 承包商信息设置DAO
 * @author 王浩羽
 *
 */
public class ContractorInfoConfigServiceImpl extends AbstractCustomerEventServiceImpl<ContractorInfoConfigVO, ContractorInfoConfigDO> implements
	IContractorInfoConfigService
{
	private IContractorInfoConfigDao contractorInfoConfigDao;
	
	public boolean addContractorInfoConfig(ContractorInfoConfigVO contractorInfoConfigVO)
	{
		if(!BlankUtil.isBlank(contractorInfoConfigVO) && !BlankUtil.isBlank(contractorInfoConfigVO.getContractorInfoName()))
		{
			if(isNameExisit(contractorInfoConfigVO.getContractorInfoName())){
				throw new BusinessException(getNameExisitExceptionInoKey(),BusinessException.WARNING);
			}
			ContractorInfoConfigDO contractorInfoConfigDO = assembleV2D(contractorInfoConfigVO,null);
			contractorInfoConfigDO.setSysInfo(createInfo(contractorInfoConfigVO.getCreator()));
			return contractorInfoConfigDao.saveObject(contractorInfoConfigDO) != null;
		}
		return false;
	}
	
	
	public boolean deleteContractorInfoConfigById(Long contractorInfoConfigId)
	{
		if(contractorInfoConfigId==null)
		{
			return false;
		}
		try
		{
			contractorInfoConfigDao.deleteObjectById(contractorInfoConfigId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("occupation:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	
	public boolean editContractorInfoConfig(ContractorInfoConfigVO contractorInfoConfigVO)
	{
		if(!BlankUtil.isBlank(contractorInfoConfigVO) && !BlankUtil.isBlank(contractorInfoConfigVO.getKeyword()))
		{
			ContractorInfoConfigDO contractorInfoConfigDO = contractorInfoConfigDao.findById(contractorInfoConfigVO.getKeyword());
			if(contractorInfoConfigDO==null)
			{
				return false;
			}
			contractorInfoConfigDO.setContractorInfoName(contractorInfoConfigVO.getContractorInfoName());
			contractorInfoConfigDO.setMainOperation(contractorInfoConfigVO.getMainOperation());
			contractorInfoConfigDO.setLinkman(contractorInfoConfigVO.getLinkman());
			contractorInfoConfigDO.setLinkmanPhone(contractorInfoConfigVO.getLinkmanPhone());
			contractorInfoConfigDO.setCompanyAddr(contractorInfoConfigVO.getCompanyAddr());
			contractorInfoConfigDO.setSequence(contractorInfoConfigVO.getSequence());
			modifyInfo(contractorInfoConfigDO.getSysInfo(), contractorInfoConfigVO.getModifier());
			return contractorInfoConfigDao.updateObject(contractorInfoConfigDO) != null;
		}
		return false;
	}
	
	protected boolean isNameExisit(String contractorInfoName){
		return contractorInfoConfigDao.findUniqueByName(contractorInfoName)!=null;
	}
	
	public Page queryContractorInfoConfigListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}
	
	
	
	public void setContractorInfoConfigDao(IContractorInfoConfigDao contractorInfoConfigDao)
	{
		this.contractorInfoConfigDao = contractorInfoConfigDao;
	}
	

	public List<ContractorInfoConfigVO> queryAllContractorInfoConfig()
	{
		List<ContractorInfoConfigDO> contractorInfoConfigDOList = contractorInfoConfigDao.findAll();
		sortList(contractorInfoConfigDOList);
		return assembleDOList2VOList(contractorInfoConfigDOList);
	}


	@Override
	protected ContractorInfoConfigDO findObjectDOById(Long id)
	{
		return contractorInfoConfigDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = contractorInfoConfigDao.queryByPage(searchVO);
		List<ContractorInfoConfigDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected int compareDO(ContractorInfoConfigDO o1, ContractorInfoConfigDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}
	
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "occupation:system_name_exist_exception";
	}
}
